
﻿using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
﻿using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using System;
using System.IO;
using System.Threading.Tasks;
using ThreeObject.ERP.EntityFrameworkCore;


using System;
using System.Threading.Tasks;
using ThreeObject.ERP.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.IO;


namespace ThreeObject.ERP.Web;

public class Program
{
    public async static Task<int> Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .WriteTo.Async(c => c.File("Logs/logs.txt"))
            .WriteTo.Async(c => c.Console())
            .CreateBootstrapLogger();

        try
        {
            Log.Information("Starting web host.");
            var builder = WebApplication.CreateBuilder(args);

            builder.Services.AddScoped<ICustomerServices, CustomersServices>();

            builder.Host
                .AddAppSettingsSecretsJson()
                .UseAutofac()
                .UseSerilog((context, services, loggerConfiguration) =>
                {
                    loggerConfiguration

                        .MinimumLevel.Debug()

                        .MinimumLevel.Information()

                        .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                        .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
                        .Enrich.FromLogContext()
                        .WriteTo.Async(c => c.File("Logs/logs.txt"))
                        .WriteTo.Async(c => c.Console())
                        .WriteTo.Async(c => c.AbpStudio(services));
                });
            await builder.AddApplicationAsync<ERPWebModule>();
            var app = builder.Build();
            await app.InitializeApplicationAsync();
            await app.RunAsync();
            return 0;
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Host terminated unexpectedly!");
            return 1;
        }
        finally
        {
            Log.CloseAndFlush();

        }

    }
    


    /// <summary>
    ///  =====数据迁移报错的时候用的========
    /// </summary>
    public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ERPDbContext>
    {
        public ERPDbContext CreateDbContext(string[] args)
        {
            // 1. 获取配置
            IConfiguration configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            // 2. 配置DbContext选项
            var builder = new DbContextOptionsBuilder<ERPDbContext>();
            builder.UseSqlServer(configuration.GetConnectionString("Default"));

            // 3. 创建DbContext实例
            return new ERPDbContext(builder.Options);
        }
    }

}

    public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ERPDbContext>
    {
        public ERPDbContext CreateDbContext(string[] args)
        {
            // 1. 获取配置
            IConfiguration configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            // 2. 配置DbContext选项
            var builder = new DbContextOptionsBuilder<ERPDbContext>();
            builder.UseSqlServer(configuration.GetConnectionString("Default"));

            // 3. 创建DbContext实例
            return new ERPDbContext(builder.Options);
        }
    }


    /// <summary>
    ///  =====数据迁移报错的时候用的========
    /// </summary>
    public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ERPDbContext>
    {
        public ERPDbContext CreateDbContext(string[] args)
        {
            // 1. 获取配置
            IConfiguration configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            // 2. 配置DbContext选项
            var builder = new DbContextOptionsBuilder<ERPDbContext>();
            builder.UseSqlServer(configuration.GetConnectionString("Default"));

            // 3. 创建DbContext实例
            return new ERPDbContext(builder.Options);
        }
    }

}

    public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ERPDbContext>
    {
        public ERPDbContext CreateDbContext(string[] args)
        {
            // 1. 获取配置
            IConfiguration configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            // 2. 配置DbContext选项
            var builder = new DbContextOptionsBuilder<ERPDbContext>();
            builder.UseSqlServer(configuration.GetConnectionString("Default"));

            // 3. 创建DbContext实例
            return new ERPDbContext(builder.Options);
        }
    }

